package com.chat.controller;

import com.chat.service.GroupService;
import com.chat.util.Result;
import com.chat.util.SecurityHolderUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 群聊控制器
 *
 * @author wb
 * @since 2024-09-14
 */

@RestController
@RequestMapping("group")
public class GroupController {


    @Autowired
    private GroupService groupService;

    @PostMapping("create")
    public Result<?> create(@RequestParam("name") String name, @RequestParam("description") String desc, @RequestParam("remark") String remark, HttpSession session) {
        return groupService.create(name, desc, remark, SecurityHolderUtil.getUserId(session)) ? Result.ok() : Result.fail();
    }

    @PostMapping("addMember")
    public Result<?> addMember(@RequestParam("chatId") String chatId, @RequestParam("friendId") Integer friendId) {
        return groupService.addMember(chatId, friendId) ? Result.ok() : Result.fail();
    }

    @GetMapping("/quit/{id}")
    public Result<?> quit(@PathVariable("id") Integer id, HttpSession session) {
        return groupService.quit(id, SecurityHolderUtil.getUserId(session)) ? Result.ok() : Result.fail();
    }

    @GetMapping("/delete/{id}")
    public Result<?> delete(@PathVariable("id") Integer id) {
        return groupService.removeById(id) ? Result.ok() : Result.fail();
    }

    @GetMapping("/members/{id}")
    public Result<?> getMembers(@PathVariable("id") String chatId) {
        Integer groupId = Integer.parseInt(chatId.substring(chatId.lastIndexOf("-") + 1));
        return Result.ok(groupService.getMembers(groupId));
    }
}
